﻿(function ($) {
    jQuery.fn.dropDown = function (operator,value){
        if(operator=='select'){
            $(this).find("input:hidden").val(value);
            var text = $(this).find("li a[ItemData="+value+"]").text();
            $(this).find("input").val(text);
        }
    }
    jQuery.fn.DropDownList = function (options) {
        var defaults = {
            InputName: "",
            ButtonText: "",
            ReadOnly: true,
            MaxHeight: -1,
            onSelect: $.noop(),
        }
        var options = $.extend(defaults, options);
        return this.each(function () {
            var o = options;
            var Obj = $(this);
            var S = "<div class='input-group'>";
            S = S + "<input type='text' class='form-control'/>";
            S = S + "<input type='hidden' name='" + o.InputName + "' />";
            S = S + "<div class='input-group-btn'>";
            S = S + "<button type='button' class='btn btn-default dropdown-toggle' data-toggle='dropdown'>" + o.ButtonText + " <span class='caret'></span></button>";
            S = S + "<ul class='dropdown-menu dropdown-menu-right' role='menu' >";
            S = S + "<li><ul class='dropdown-menu ' style='display:inherit;position:inherit;top:0;float:inherit;padding:0;border:0px;border-radius:0px;-webkit-box-shadow: inherit;box-shadow: inherit;'>";
            var SelText, SelData;
            if (o.Sections !== undefined) {
                $.each(o.Sections, function (n, value) {
                    if (n > 0) { S = S + "<li class='divider'></li>"; }
                    if (value.ItemHeader !== undefined) { S = S + "<li class='dropdown-header'>" + value.ItemHeader + "</li>"; }
                    CreateItem(value);
                });
            }
            else {
                CreateItem(o);
            }

            function CreateItem(Items) {
                $.each(Items.Items, function (n, Item) {
                    if (Item.ItemData === undefined) { Item.ItemData = Item.ItemText; }
                    S = S + "<li><a href='#' ItemData='" + Item.ItemData + "' >" + Item.ItemText + "</a></li>";
                    if (Item.Selected == true) { SelText = Item.ItemText; SelData = Item.ItemData; }
                });
            }

            S = S + "</ul></li></ul></div></div>";
            Obj.html(S);

            Obj.find(".dropdown-menu").css("min-width", "0");
            if (options.dropwidth) {
                Obj.find(".dropdown-menu").width(options.dropwidth);
            } else {
                Obj.find(".dropdown-menu").width(Obj.width());
            }
            var Input = Obj.find("input");
            var hidden = Obj.find("input:hidden");
            if (SelText != "") { SetData(SelText, SelData); }
            Obj.find("a").bind("click", function (e) {
                SetData($(this).html(), $(this).attr("ItemData"));
            });
            if (o.ReadOnly == true) {
                Input.bind("cut copy paste keydown", function (e) { e.preventDefault(); });
            }
            if (o.MaxHeight > 0) {
                var UL = Obj.find("ul[style]");
                UL.css({ 'max-height': o.MaxHeight, 'overflow': 'auto' });
            }
            function SetData(Text, Data) {
                Input.val(Text);
                hidden.val(Data);
                if (o.onSelect) { o.onSelect(o.InputName, Data); }
            }
        });
    }
})(jQuery);